package com.android.youwenchuang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.android.youwenchuang.bean.Goods;
import com.android.youwenchuang.bean.HomeBean;
import com.android.youwenchuang.bean.HomeGoodsBean;
import com.android.youwenchuang.databinding.FragmentHomeBinding;
import com.android.youwenchuang.presenter.HomePresenter;
import com.android.youwenchuang.ui.activity.GoodsDetailsActivity;
import com.android.youwenchuang.ui.activity.SearchActivity;
import com.android.youwenchuang.ui.adapter.HomeAdapter;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding viewBinding;

    private HomePresenter homePresenter = new HomePresenter();
    private HomeAdapter homeAdapter = new HomeAdapter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentHomeBinding.inflate(LayoutInflater.from(requireContext()));
        return viewBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        GridLayoutManager gl = new GridLayoutManager(requireContext(), 2);
        gl.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 0) return 2;
                return 1;
            }
        });
        viewBinding.rvList.setLayoutManager(gl);
        viewBinding.rvList.setAdapter(homeAdapter);
        List<MultiItemEntity> data = new ArrayList<>();
        List<String> pics = new ArrayList<>();
        pics.add("https://preview.qiantucdn.com/58pic/20220319/00i58PIC622mWWtIz58PIC2yX_PIC2018_PIC2018.jpg%21qt720_water_jpg");
        pics.add("https://file.moyublog.com/d/file/2021-02-03/1dae7ca83504f6fdf6d6738ee09e27a1.jpg");
        data.add(new HomeBean(pics));
        data.addAll(homePresenter.getGoodsList());
        //详情页
        homeAdapter.setNewInstance(data);
        homeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (homeAdapter.getItemViewType(position) == 1) {
                    Intent intent = new Intent(requireContext(), GoodsDetailsActivity.class);
                    Goods goods = ((HomeGoodsBean) homeAdapter.getItem(position)).getGoods();
                    intent.putExtra("data", GsonUtils.toJson(goods));
                    startActivity(intent);
                }
            }
        });
        //搜索框
        viewBinding.llSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtils.startActivity(SearchActivity.class);
            }
        });

    }

}
